package cn.daniellee.plugin.rb.core;

import cn.daniellee.plugin.rb.ResidenceBungee;
import com.bekvon.bukkit.residence.Residence;
import com.bekvon.bukkit.residence.containers.Flags;
import com.bekvon.bukkit.residence.containers.ResidencePlayer;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class BungeeCore {

	public static String serverName = "Invalid";

	public static boolean fetchedName = false;

	// 领地Map，Key=领地名，Value=所在服务器
	public static Map<String, String> residenceMap = new ConcurrentHashMap<>();

	// 待传送Map，Key=玩家名，Value=目标领地
	public static Map<String, String> teleportMap = new ConcurrentHashMap<>();

	public static void fetchServerName(Player player) {
		if (ResidenceBungee.getInstance().isBungeecord()) {
			ByteArrayDataOutput out = ByteStreams.newDataOutput();
			out.writeUTF("GetServer");
			player.sendPluginMessage(ResidenceBungee.getInstance(), "BungeeCord", out.toByteArray());
			BungeeCore.fetchedName = true;
		}
	}

	// 广播当前服务器领地列表
	public static void sendResListMessage(String targetServer) {
		ByteArrayDataOutput out = ByteStreams.newDataOutput();
		out.writeUTF("Forward");
		out.writeUTF(targetServer);
		out.writeUTF("ResidenceBungee");
		ByteArrayOutputStream msgBytes = new ByteArrayOutputStream();
		DataOutputStream msgOut = new DataOutputStream(msgBytes);
		try {
			msgOut.writeUTF("ResList");
			msgOut.writeUTF(serverName);
			msgOut.writeUTF(StringUtils.join(Residence.getInstance().getResidenceManager().getResidences().entrySet().stream().filter(item -> item.getValue().getPermissions().has(Flags.tp, false, false)).map(Map.Entry::getKey).toList(), ','));
		} catch (IOException ignored){}
		out.writeShort(msgBytes.toByteArray().length);
		out.write(msgBytes.toByteArray());
		Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
		if (player != null) {
			player.sendPluginMessage(ResidenceBungee.getInstance(), "BungeeCord", out.toByteArray());
		}
	}

	// 广播请求其他服务器向本服务器发送领地列表
	public static void sendResRequestMessage() {
		ByteArrayDataOutput out = ByteStreams.newDataOutput();
		out.writeUTF("Forward");
		out.writeUTF("ALL");
		out.writeUTF("ResidenceBungee");
		ByteArrayOutputStream msgBytes = new ByteArrayOutputStream();
		DataOutputStream msgOut = new DataOutputStream(msgBytes);
		try {
			msgOut.writeUTF("ResRequest");
			msgOut.writeUTF(serverName);
		} catch (IOException ignored){}
		out.writeShort(msgBytes.toByteArray().length);
		out.write(msgBytes.toByteArray());
		Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
		if (player != null) {
			player.sendPluginMessage(ResidenceBungee.getInstance(), "BungeeCord", out.toByteArray());
		}
	}
}
